<template>
  <div class="app-container">
    <div class="form" style="max-width: 70%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="商品描述" prop="desc">
          <el-input type="textarea" :rows="3" v-model="ruleForm.desc">
          </el-input>
        </el-form-item>
        <el-form-item label="商品品类" prop="cate">
          <CateSelect v-model="ruleForm.cate" />
        </el-form-item>
        <el-form-item label="是否热销" prop="hot">
          <el-switch v-model="ruleForm.delivery"></el-switch>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input-number
            v-model="ruleForm.price"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="商品图片" prop="img">
          <SingleImage v-model="ruleForm.img" />
        </el-form-item>
      </el-form>

      <div class="btns">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >添加</el-button
        >
        <el-button>取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import CateSelect from "./components/CateSelect";
import SingleImage from "@/components/Upload/SingleImage";
export default {
  name: "GoodForm",
  components: { CateSelect, SingleImage },
  data() {
    return {
      ruleForm: {
        img: "",
        name: "",
        price: "",
        cate: "",
        desc: "",
        hot: false,
      },
      rules: {
        name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          { min: 2, message: "商品名称不能少于两个字", trigger: "blur" },
          { max: 18, message: "商品名称不能多于十八个字", trigger: "blur" },
        ],
        cate: [
          { required: true, message: "请选择商品类别", trigger: "change" },
        ],
      },
    };
  },
  activated() {
    this.ruleForm = this.$route.params.row;
    console.log(this.ruleForm);
  },
  methods: {
    submitForm(formName) {
      console.log("提交", this.ruleForm);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$api.fetchGoodForm(this.ruleForm).then(() => {
            console.log("商品添加成功");
            this.$message({
              message: "商品添加成功",
              type: "success",
              duration: 1000,
              onClose: () => {
                this.$router.replace("/good/list");
              },
            });
          });
        } else {
          this.$message({
            message: "错误提交",
            type: "error",
            duration: 2000,
          });
          return false;
        }
      });
    },
  },
};
</script>


<style lang="scss" scoped>
.form {
  padding-bottom: 80px;
  position: relative;
}
.btns {
  position: absolute;
  bottom: 5px;
  height: 60px;
  left: -20px;
  right: -450px;
  z-index: 9999;
  background-color: white;
  text-align: center;
  line-height: 60px;
  border-top: 1px solid #eee;
}
</style>